package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.entity.User;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.view.BusinessDataVO;
import com.sky.view.DishOverViewVO;
import com.sky.view.OrderOverViewVO;
import com.sky.view.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //查询任意时间段内数据
    @Override
    public BusinessDataVO getBusinessData(LocalDate begin,LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<Orders>()
                .eq(Orders::getStatus, Orders.COMPLETED);
        Long total = ordersMapper.selectCount(queryWrapper);
        //有效订单数
        Integer valid;
        //营业额
        Double turnover;
        //订单完成率
        Double completionRate;
        //平均客单价
        Double unitPrice;
        if(total!=0){
            LambdaQueryWrapper<Orders> queryWrapper1 = new LambdaQueryWrapper<Orders>()
                    .eq(Orders::getStatus, Orders.COMPLETED)
                    .between(Orders::getOrderTime, beginTime, endTime);
            List<Orders> ordersList = ordersMapper.selectList(queryWrapper1);
            if (!ordersList.isEmpty()){
                valid = ordersList.size();
                turnover = ordersList.stream().map(Orders::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add).doubleValue();
                completionRate = BigDecimal.valueOf(valid).divide(BigDecimal.valueOf(total), 4, RoundingMode.HALF_UP).doubleValue();
                unitPrice = BigDecimal.valueOf(turnover).divide(BigDecimal.valueOf(valid), 4, RoundingMode.HALF_UP).doubleValue();
            }else {
                valid = 0;
                turnover= (double) 0;
                completionRate= (double) 0;
                unitPrice= (double) 0;
            }
        }else {
            valid = 0;
            turnover= (double) 0;
            completionRate= (double) 0;
            unitPrice= (double) 0;
        }
        LambdaQueryWrapper<User> queryWrapper2 = new LambdaQueryWrapper<User>()
                .between(User::getCreateTime, beginTime, endTime);
        //新增用户数
        Integer newUser = userMapper.selectCount(queryWrapper2).intValue();
        return new BusinessDataVO(turnover,valid,completionRate,unitPrice,newUser);
    }

    //查询订单管理数据
    @Override
    public OrderOverViewVO getOrderOverView() {
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //待接单
        queryWrapper.eq(Orders::getStatus, Orders.TO_BE_CONFIRMED);
        Long toBeConfirmed = ordersMapper.selectCount(queryWrapper);
        //待派送
        queryWrapper.clear();
        queryWrapper.eq(Orders::getStatus,Orders.CONFIRMED);
        Long confirmed = ordersMapper.selectCount(queryWrapper);
        //已完成
        queryWrapper.clear();
        queryWrapper.eq(Orders::getStatus,Orders.COMPLETED);
        Long completed = ordersMapper.selectCount(queryWrapper);
        //已取消
        queryWrapper.clear();
        queryWrapper.eq(Orders::getStatus,Orders.CANCELLED);
        Long canceled = ordersMapper.selectCount(queryWrapper);
        //全部
        Long total = ordersMapper.selectCount(null);
        return new OrderOverViewVO(toBeConfirmed,confirmed,completed,canceled,total);
    }

    //查询菜品总览
    @Override
    public DishOverViewVO getDishOverView() {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //停售
        queryWrapper.eq(Dish::getStatus, 0);
        Long discontinued = dishMapper.selectCount(queryWrapper);
        //起售
        queryWrapper.clear();
        queryWrapper.eq(Dish::getStatus,1);
        Long sold = dishMapper.selectCount(queryWrapper);
        return new DishOverViewVO(sold,discontinued);
    }

    //查询套餐总览
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //停售
        queryWrapper.eq(Setmeal::getStatus, 0);
        Long discontinued = setmealMapper.selectCount(queryWrapper);
        //起售
        queryWrapper.clear();
        queryWrapper.eq(Setmeal::getStatus,1);
        Long sold = setmealMapper.selectCount(queryWrapper);
        return new SetmealOverViewVO(sold,discontinued);
    }

}
